package aiaudio.storage.simple;

import aiaudio.storage.api.IArtist;
import aiaudio.storage.api.IAudioTestSet;
import aiaudio.storage.api.IUser;
import aiaudio.storage.api.InternalException;
import aiaudio.storage.api.UserArtistTuple;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

/**
 *
 * @author Nastya
 */
public class AudioTestSet implements IAudioTestSet {

    private List<IUser> users;
    private List<IArtist> artists;
    private HashMap<UserArtistTuple, Double> ratings;

    AudioTestSet(List<IUser> users, List<IArtist> artists,
            HashMap<UserArtistTuple, Double> ratings) {
        this.users = users;
        this.artists = artists;
        this.ratings = ratings;
    }

    @Override
    public Iterator<IArtist> getArtists() {
        return artists.iterator();
    }

    @Override
    public Iterator<IUser> getUsers() {
        return users.iterator();
    }

    @Override
    public double getRatings(IArtist artist, IUser user) throws InternalException {
        UserArtistTuple tuple = new UserArtistTuple(user, artist);
        if (ratings.containsKey(tuple)) {
            return ratings.get(tuple);
        }

        return 0;
    }

    @Override
    public Iterator<UserArtistTuple> getAvaliableTuples() {
        List<UserArtistTuple> result = new ArrayList<UserArtistTuple>();
        Set<Entry<UserArtistTuple, Double>> entrySet = ratings.entrySet();
        for (Entry<UserArtistTuple, Double> entry : entrySet) {
            result.add(entry.getKey());
        }

        return result.iterator();
    }

    @Override
    public String toString() {
        return "AudioTestSet{" + "users=" + users + ",\r\n artists="
                + artists + ",\r\n ratings=" + ratings + '}';
    }
}
